/**
* @Title: institution_list_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-chenyonghong001
* @date 2014年10月17日 下午2:38:47
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('institutionListOtherCtrl', ['$scope','CommunityService','$http','$modal','$state','InstitutionListService','BaseURL','Modal',
	                                function($scope,CommunityService,$http,$modal,$state,institutionListService,baseUrl,Modal){
		//判断是否已经查询过 机构信息 
		if (institutionListService.institutionMedicalOrgInfoList) {
			 $scope.medicalOrgInfoList = institutionListService.institutionMedicalOrgInfoList;
		}
		else {
			institutionListService.initMedicalOrgInfoList("","","0").success(function(response){
	            $scope.medicalOrgInfoList = response.medicalOrgList;
	            institutionListService.institutionMedicalOrgInfoList = response.medicalOrgList;
			});
		}
		$scope.communityService = CommunityService;
		//	codeNo=350200&codeType=region&regionType=3
		CommunityService.getBaseCode('{codeType:\'regionXiamen\',codeNo:\'350200\',regionType:\'3\'}');
      //  $scope.UrlParams = $routeParams;
		CommunityService.initCodeListDatas();
		$scope.institutionListParam = {
            page:"1",
            pageSize:"20"
        };
    	$scope.currentTab = null;

		$scope.tabs = [  {name:'定点医院',currentTab:'0',type:'institutionInfoOther.institutionOther'},
			             {name:'定点药店',currentTab:'1',type:'institutionInfoOther.pharmacyOther'}
		            ];
		var type = 0;
        //分页监听
        $scope.$on('pageTool.skipPage',function(e,v){
           
            $scope.onSubmit(v);
        });
		$scope.currentTab = $scope.tabs[0];
		// 表头以及json对应的属性名称
		$scope.columns = [ {
			label : '机构编号',
			map : 'providerCode',
			width : '12%'
		}, {
			label : '机构名称',
			map : 'providerName',
			width : '18%',
			 cellTemplate:'<a  ng:bind="data.providerName" '+
			 	'ui-sref="institutionInfoOther.institutionDetailOther({providerid:data.providerId,type:0})"></a>'}, 
		{
			label : '机构等级',
			map : 'hospitalLevel',
			width : '12%'
		}, {
			label : '机构类别',
			map : 'organizationType',
			width : '10%'
		}, {
			label : '医保负责人',
			map : 'insuranceCharger',
			width : '12%'
		}, {
			label : '联系电话',
			map : 'chargerPhone',
			width : '10%'
		}, {
			label : '地址',
			map : 'fullAddress',
			width : '26%'
		}];
        $scope.institutionList = {};

        if (institutionListService.institutionListParam) {
            $scope.institutionListParam = institutionListService.institutionListParam;
        }
        $scope.authority={
        		add:1,
        		update:1,
        		deleted:1
        }
        $scope.onSubmit = function(page){
        	if(!page){
        		page=1;
        	}
        	 $scope.institutionListParam.page = page;
        	// $scope.currentPage = page;
//            var pageInfo = $scope.pageInfo;
        	$scope.institutionListParam.providertype = '0';
            var params = $scope.institutionListParam;
            institutionListService.institutionListParam = $scope.institutionListParam;

            institutionListService.query(params).success(function(res){
            	if(res){
            		$scope.tableData = res.result;
        			if(res.totals){
        				$scope.totalCount = res.totals.total;
        			}else{
        				$scope.totalCount =0;
        			}
        			$scope.authority=res.authority;
            	}
          
//              //配置分页参数
//              $scope.pageInfo = {
//                  curPage:params.page,
//                  total:data.totals.total,
//                  limit: params.pageSize,
//                  loadData:$scope.queryInstitutionList
//              };
			});


        };
        
		//切换tab页
		$scope.tabClick = function(tab) {
			$state.go(tab.type);
		}
        /**
         * 机构详细信息--新增
         * @param params
         */
		
		$scope.addInstitution = function () {
			$state.go('institutionInfoOther.institutionDetailOther',{
				type:0
			});
		};
		
//        $scope.institutionDetail = {};
//        $scope.addProvider = function(){
//            var params = $scope.institutionDetail;
//            institutionListService.createProvider(params,function(data){
//                alert(data.msg);
//            });
//        };

        /**
         * 列表复选框--全选控制
         * @type {{}}
         */
//        $scope.institutionIdList = "";
//        $scope.getInstitutionIdList = function(domName,isArray,prefix){
//            $scope.institutionIdList = Utils.getCheckBoxValue(domName,isArray,prefix);
//        };

        /**
         *机构列表--删除
         * @param params
         */
//        $scope.deleteInstitutionList = function(){
//          	dialog.confirm("确认要删除该医院信息吗？若删除，其相关的医疗设备、违规、联系信息、账号信息、诊疗项目、医生信息也将一并被删除！",function(){
//                var params = {ids:$scope.institutionIdList};
//                institutionListService.deleteInstitutionList(params,function(data){
//                	dialog.alert(data.msg);
//                	  $scope.institutionIdList ="";
//                      document.getElementById('institutionIdList').checked = false;
//                    $scope.queryInstitutionList($scope.agreementListParam);
//                });
//          	})
//        
//        };
		
		
	 var totalChecks = [];
       $scope.checkRows = [];

        function findCheck(d) {
            for(var i=0;i< $scope.checkRows .length;i++) {
                var checkedData =  $scope.checkRows [i];
                if(checkedData.itemId == d.itemId) {
                    return i;
                }
            }
            return -1;
        }

        $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                $scope.checkRows.push(d);
            }
        });

        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
                $scope.checkRows.splice(index,1);
            }
        });
        $scope.onReset=function(){
        	$scope.institutionListParam.providerName="";
        	$scope.institutionListParam.providerCode="";
        	$scope.institutionListParam.addressCounty="";
        };
		
		$scope.deleteInstitution= function(){
		      var checkRows =$scope.checkRows;
	            if (checkRows.length == 0) {
	                Modal.alert("请勾选删除项！");
	                return;
	            }else
	            {
	                var ids = '';
	                angular.forEach(checkRows,function(totalCheck,i){
	                    ids+="'"+totalCheck.providerId+"',";
	                });
	                var  params= {
	                ids : ids
	                }
	            	Modal.confirm('确认删除吗？',function(){
	                    $http.post(baseUrl + '/hcpms/provider/delete.do',params).success(function(data) {
		                    if(data.state=='0')
		                    {
		                        Modal.alert(data.msg);
		                    }else
		                    {
		                        Modal.alert("删除失败！");
		                    }
		                    $scope.onSubmit();
		                })
	            		
	            	})
	    
	            }
		}
		
		 $scope.onSubmit();
    }
	]);
})();